package com.dianping.cat.home.storage.alert.transform;

import com.dianping.cat.home.storage.alert.IEntity;
import com.dianping.cat.home.storage.alert.IVisitor;
import com.dianping.cat.home.storage.alert.entity.Detail;
import com.dianping.cat.home.storage.alert.entity.Machine;
import com.dianping.cat.home.storage.alert.entity.Operation;
import com.dianping.cat.home.storage.alert.entity.Storage;
import com.dianping.cat.home.storage.alert.entity.StorageAlertInfo;
import com.dianping.cat.home.storage.alert.entity.Target;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;
  
/*  40 */   public DefaultXmlBuilder() { this(false); }



/*  44 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }
  
  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  48 */     this.m_compact = compact;
/*  49 */     this.m_sb = sb;
/*  50 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }
  
  public String buildXml(IEntity<?> entity) {
/*  54 */     entity.accept(this.m_visitor);
/*  55 */     return this.m_sb.toString();
  }
  
  protected void endTag(String name) {
/*  59 */     this.m_level--;

/*  61 */     indent();
/*  62 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  
/*  66 */   protected String escape(Object value) { return escape(value, false); }

  
  protected String escape(Object value, boolean text) {
/*  70 */     if (value == null) {
/*  71 */       return null;
    }
    
/*  74 */     String str = toString(value);
/*  75 */     int len = str.length();
/*  76 */     StringBuilder sb = new StringBuilder(len + 16);

    for (int i = 0; i < len; i++) {
/*  79 */       char ch = str.charAt(i);

/*  81 */       switch (ch) {
        case '<':
/*  83 */           sb.append("&lt;");
          break;
        case '>':
/*  86 */           sb.append("&gt;");
          break;
        case '&':
/*  89 */           sb.append("&amp;");
          break;
        case '"':
/*  92 */           if (!text) {
/*  93 */             sb.append("&quot;");
            break;
          }
        default:
/*  97 */           sb.append(ch);
          break;
      }
    
    }
/* 102 */     return sb.toString();
  }
  
  protected void indent() {
/* 106 */     if (!this.m_compact) {
/* 107 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 108 */         this.m_sb.append("   ");
      }
    }
  }

  
/* 114 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 118 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 122 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }

  
  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 126 */     indent();

/* 128 */     this.m_sb.append('<').append(name);

/* 130 */     int len = nameValues.length;

/* 132 */     for (int i = 0; i + 1 < len; i += 2) {
/* 133 */       Object attrName = nameValues[i];
/* 134 */       Object attrValue = nameValues[i + 1];

/* 136 */       if (attrValue != null) {
/* 137 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }
    
/* 141 */     if (dynamicAttributes != null) {
/* 142 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 143 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }
    
/* 147 */     if (text != null && closed) {
/* 148 */       this.m_sb.append('>');
/* 149 */       this.m_sb.append(escape(text, true));
/* 150 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 152 */       if (closed) {
/* 153 */         this.m_sb.append('/');
      } else {
/* 155 */         this.m_level++;
      }
      
/* 158 */       this.m_sb.append(">\r\n");
    }
  }

  
  protected String toString(Object value) {
/* 164 */     if (value instanceof String)
/* 165 */       return (String)value; 
/* 166 */     if (value instanceof Collection) {
/* 167 */       Collection<Object> list = (Collection)value;
/* 168 */       StringBuilder sb = new StringBuilder(32);
/* 169 */       boolean first = true;

/* 171 */       for (Object item : list) {
/* 172 */         if (first) {
/* 173 */           first = false;
        } else {
/* 175 */           sb.append(',');
        }
        
/* 178 */         if (item != null) {
/* 179 */           sb.append(item);
        }
      }
      
/* 183 */       return sb.toString();
/* 184 */     }  if (value.getClass().isArray()) {
/* 185 */       int len = Array.getLength(value);
/* 186 */       StringBuilder sb = new StringBuilder(32);
/* 187 */       boolean first = true;

/* 189 */       for (int i = 0; i < len; i++) {
/* 190 */         Object item = Array.get(value, i);

/* 192 */         if (first) {
/* 193 */           first = false;
        } else {
/* 195 */           sb.append(',');
        }
        
/* 198 */         if (item != null) {
/* 199 */           sb.append(item);
        }
      }
      
/* 203 */       return sb.toString();
    }
    
/* 206 */     return String.valueOf(value);
  }
  
  protected String toString(Date date, String format) {
/* 210 */     if (date != null) {
/* 211 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 213 */     return null;
  }



  
/* 219 */   public void visitDetail(Detail detail) { startTag("detail", true, null, new Object[] { "content", detail.getContent(), "level", Integer.valueOf(detail.getLevel()) }); }


  
  public void visitMachine(Machine machine) {
/* 224 */     startTag("machine", null, new Object[] { "id", machine.getId(), "level", Integer.valueOf(machine.getLevel()), "count", Integer.valueOf(machine.getCount()) });

/* 226 */     if (!machine.getOperations().isEmpty()) {
/* 227 */       for (Operation operation : machine.getOperations().values()) {
/* 228 */         operation.accept(this.m_visitor);
      }
    }
    
/* 232 */     endTag("machine");
  }

  
  public void visitOperation(Operation operation) {
/* 237 */     startTag("operation", null, new Object[] { "id", operation.getId(), "level", Integer.valueOf(operation.getLevel()), "count", Integer.valueOf(operation.getCount()) });

/* 239 */     if (!operation.getTargets().isEmpty()) {
/* 240 */       for (Target target : operation.getTargets().values()) {
/* 241 */         target.accept(this.m_visitor);
      }
    }
    
/* 245 */     endTag("operation");
  }

  
  public void visitStorage(Storage storage) {
/* 250 */     startTag("storage", null, new Object[] { "id", storage.getId(), "level", Integer.valueOf(storage.getLevel()), "count", Integer.valueOf(storage.getCount()) });

/* 252 */     if (!storage.getMachines().isEmpty()) {
/* 253 */       for (Machine machine : storage.getMachines().values()) {
/* 254 */         machine.accept(this.m_visitor);
      }
    }
    
/* 258 */     endTag("storage");
  }

  
  public void visitStorageAlertInfo(StorageAlertInfo storageAlertInfo) {
/* 263 */     startTag("storage-alert-info", null, new Object[] { "id", storageAlertInfo.getId(), "start-time", toString(storageAlertInfo.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "end-time", toString(storageAlertInfo.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 265 */     if (!storageAlertInfo.getStorages().isEmpty()) {
/* 266 */       for (Storage storage : storageAlertInfo.getStorages().values()) {
/* 267 */         storage.accept(this.m_visitor);
      }
    }
    
/* 271 */     endTag("storage-alert-info");
  }

  
  public void visitTarget(Target target) {
/* 276 */     startTag("target", null, new Object[] { "id", target.getId(), "title", target.getTitle(), "level", Integer.valueOf(target.getLevel()), "count", Integer.valueOf(target.getCount()) });

/* 278 */     if (!target.getDetails().isEmpty()) {
/* 279 */       for (Detail detail : target.getDetails()) {
/* 280 */         detail.accept(this.m_visitor);
      }
    }
    
/* 284 */     endTag("target");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/storage/alert/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */